package com.lipert.sk2;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.plaf.SliderUI;

public class GUI extends JFrame implements ActionListener {

	public static int PUBLIC_PORT = 9998;
	public static int PRIVATE_PORT = 9999;

	private JFrame frame;
	private JTabbedPane tabbedPane;
	private ImageIcon icon1;
	private JPanel panel1;
	private JPanel panel2;
	private JPanel panel3;
	public static JTextArea publicHistoryArea;
	public static JScrollPane publicHistoryPane;
	private JTextField publicMessageText;
	private JButton publicSendButton;
	public static JList usersList;
	public static DefaultListModel listModel;
	public static JScrollPane usersPane;
	public static JTextArea privateHistoryArea;
	public static JScrollPane privateHistoryPane;
	private JTextField privateMessageText;
	private JButton privateSendButton;
	private JButton talkWithButton;
	private JLabel ipLabel;
	private JLabel portLabel;
	private JButton logInButton;
	public static JTextField ipText;
	public static JTextField portText;
	private JLabel nickLabel;
	private JButton logOutButton;
	public static DefaultListModel listModel2;
	public static JList usersList2;
	public static JScrollPane usersPane2;
	public static JTextField nickText;
	public static ServerReceiver serverReceiver;

	public void init() {

		tabbedPane = new JTabbedPane();
		icon1 = new ImageIcon("/home/communication-icon.png");

		panel1 = new JPanel();// makeTextPanel("Panel #1");
		tabbedPane.addTab("Czat publiczny", icon1, panel1, "Does nothing");
		tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);

		panel2 = new JPanel();
		tabbedPane.addTab("Rozmowa prywatna", icon1, panel2,
				"Does twice as much nothing");
		tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);

		panel3 = new JPanel();
		tabbedPane.addTab("Ustawienia", icon1, panel3, "Still does nothing");
		tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);

		// czat publiczny
		publicHistoryArea = new JTextArea(10, 30);
		publicHistoryPane = new JScrollPane(publicHistoryArea);
		panel1.add(publicHistoryPane);

		publicHistoryArea.setEditable(false);

		publicMessageText = new JTextField(30);
		panel1.add(publicMessageText);

		publicSendButton = new JButton("Wyślij");
		publicSendButton.setPreferredSize(new Dimension(334, 30));
		publicSendButton.addActionListener((ActionListener) this);
		panel1.add(publicSendButton);

		listModel = new DefaultListModel();
		//listModel.addElement("Jane Doe");
		//listModel.addElement("John Smith");
		//listModel.addElement("Kathy Green");

		usersList = new JList(listModel);
		usersPane = new JScrollPane(usersList);
		usersPane.setPreferredSize(new Dimension(334, 100));
		panel1.add(usersPane);

		// rozmowa prywatna
		privateHistoryArea = new JTextArea(10, 30);
		privateHistoryPane = new JScrollPane(privateHistoryArea);
		panel2.add(privateHistoryPane);

		privateHistoryArea.setLineWrap(true);

		privateHistoryArea.setEditable(false);

		privateMessageText = new JTextField(30);
		panel2.add(privateMessageText);

		privateSendButton = new JButton("Wyślij");
		privateSendButton.setPreferredSize(new Dimension(334, 30));
		privateSendButton.addActionListener(this);
		panel2.add(privateSendButton);

		listModel2 = new DefaultListModel();
	//	listModel.addElement("Jane Doe");
		//listModel.addElement("John Smith");
		//listModel.addElement("Kathy Green");

		usersList2 = new JList(listModel2);
		usersPane2 = new JScrollPane(usersList2);
		usersPane2.setPreferredSize(new Dimension(334, 100));
		panel2.add(usersPane2);

		talkWithButton = new JButton("Rozmawiaj");
		talkWithButton.setPreferredSize(new Dimension(334, 30));
		panel2.add(talkWithButton);

		// ustawienia

		ipLabel = new JLabel("Adres IP serwera");
		panel3.add(ipLabel);

		ipText = new JTextField("192.168.0.11",20);
		panel3.add(ipText);

		portLabel = new JLabel("Port serwera");
		panel3.add(portLabel);

		portText = new JTextField("8888",20);
		panel3.add(portText);

		nickLabel = new JLabel("Nick");
		panel3.add(nickLabel);

		nickText = new JTextField("Tomek",30);
		panel3.add(nickText);

		logInButton = new JButton("Połącz z serwerem");
		logInButton.addActionListener(this);
		panel3.add(logInButton);
		
		logOutButton = new JButton("Rozłącz");
		logOutButton.addActionListener(this);
		panel3.add(logOutButton);

		frame = new JFrame("Uberkomunikator");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(tabbedPane);
		frame.setSize(420, 500);
		frame.setVisible(true);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == publicSendButton) {

			
			// publicHistoryArea.append(messageText.getText() + "\n");
			// messageText.setText("");

			Sender a = new Sender();
			for(int i = 0; i<ServerReceiver.addressList.size(); i++){
				
			a.connection(ServerReceiver.addressList.get(i), PUBLIC_PORT);
			a.sendPublic((publicMessageText.getText()).getBytes());
			}
		
			publicMessageText.setText("");


			// System.out.println(privateMessageText.getT);
		}
		if (e.getSource() == privateSendButton) {

			 privateHistoryArea.append("Ja: " + privateMessageText.getText() + "\n");

			Sender a = new Sender();
			int index = ServerReceiver.nickList.indexOf(usersList2.getSelectedValue());
			System.out.println("index: "+index);
			a.connection(ServerReceiver.addressList.get(index), PRIVATE_PORT);

			// System.out.println((privateMessageText.getText()));

			a.sendPrivate((privateMessageText.getText()).getBytes());
			privateMessageText.setText("");

		}

		if (e.getSource() == logInButton) {
			serverReceiver = new ServerReceiver();
			serverReceiver.start();
			
		}
		
		if (e.getSource() == logOutButton) {
			ServerReceiver.a.sendToServer("EXIT");
			try {
				ServerReceiver.serverSocket.close();
				serverReceiver.interrupt();
				System.exit(0);
			} catch (IOException e1) {
				
				System.out.println("bye");
			}
		}

	}

	public static void main(String[] args) {
		GUI okno = new GUI();
		okno.init();
		Receiver privateReceiver = new Receiver();
		PublicReceiver publicReceiver = new PublicReceiver();
		privateReceiver.start();
		publicReceiver.start();
	}

}
